//
//  CS_Page_InformView.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2018/12/19.
//

#import "CS_Page_InformView.h"

#define HEIGHT_SELF 40.0

@interface CS_Page_InformView()

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, assign) BOOL isAnimationRequired;

@property (nonatomic, copy) InformClickBlock clickHandler;

@property (nonatomic, strong) UIButton *button;

@end

@implementation CS_Page_InformView

+(id)infromView:(NSArray *)informations{
    
    CS_Page_InformView *informView = [CS_Page_InformView new];
    informView.informations = informations;
    
    return informView;
    
}

+(id)infromView:(NSArray *)informations clickHandler:(InformClickBlock)clickHandler{
    
    CS_Page_InformView *informView = [CS_Page_InformView new];
    informView.informations = informations;
    informView.clickHandler = clickHandler;
    
    return informView;
    
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    self.backgroundColor = [UIColor colorFromHexCode:@"fffdee"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tubiao-.png"]];
    imageView.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, (HEIGHT_SELF - 22)/2.0, 22, 22);
    
    [self addSubview:self.label];
    
    UIView *view1 = [UIView new];
    view1.frame = CGRectMake(0, 0, CELL_EDIT_RIGHT_MARGIN + 22 + 10, HEIGHT_SELF);
    view1.backgroundColor = [UIColor colorFromHexCode:@"fffdee"];
    [self addSubview:view1];
    
    UIView *view2 = [UIView new];
    view2.frame = CGRectMake(kWindowW - CELL_EDIT_RIGHT_MARGIN, 0, CELL_EDIT_RIGHT_MARGIN, HEIGHT_SELF);
    view2.backgroundColor = [UIColor colorFromHexCode:@"fffdee"];
    [self addSubview:view2];
    
    [self addSubview:imageView];
    
    self.button.frame = CGRectMake(0, 0, kWindowW, HEIGHT_SELF);
    self.frame = CGRectMake(0, primaryY, kWindowW, HEIGHT_SELF);
    [self addSubview:self.button];
    
    [self startAnimationIfNeededWithLabel:self.label andLabelWidth:self.label.frame.size.width];
    
    [superView addSubview:self];
    
    return primaryY + HEIGHT_SELF;
    
}

-(UILabel *)label{
    
    if (_label == nil) {
        _label = [[UILabel alloc] init];
        _label.textColor = [UIColor colorFromHexCode:@"f26830"];
        _label.font = [UIFont systemFontOfSize:FONT_SIZE_3];
        _label.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + 22 + 10, 0, kWindowW - (2 * CELL_EDIT_RIGHT_MARGIN + 22 + 10), HEIGHT_SELF);
        
        NSString *information = @"";
        for (int i = 0; i < self.informations.count; i++) {
            
            information = [NSString stringWithFormat:@"%@           %@", information, [self.informations objectAtIndex:i]];
            
        }
        
        _label.text = information;
    }
    return _label;
    
}

-(void)startAnimationIfNeededWithLabel:(UILabel *)label andLabelWidth:(CGFloat)width{
    
    //取消、停止所有的动画
    [label.layer removeAllAnimations];
    CGSize textSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];//获取赋值后的label的size
    CGRect lframe = label.frame;
    lframe.size.width = textSize.width;
    label.frame = lframe;
    if (textSize.width > width) {//文字长度大于label长度
        float offset = textSize.width;//滚动的长度，大家可以试着改动这个值，寻找适合自己的效果
        label.transform = CGAffineTransformMakeTranslation(kWindowW, 0);
        [UIView animateWithDuration:20.0 delay:0.0 options:UIViewAnimationOptionRepeat //动画重复
         |UIViewAnimationOptionTransitionFlipFromRight //动画从左到右
         |UIViewAnimationOptionCurveLinear //动画的时间曲线，线性可实现平滑滚动
                         animations:^{
                             label.transform = CGAffineTransformMakeTranslation(-offset, 0);
                         }
                         completion:^(BOOL finished) {
                             
                         }
         ];
    }
    
}

/**
 * 唤醒动画
 */
-(void)resumeAnimation{
    [self startAnimationIfNeededWithLabel:self.label andLabelWidth:(kWindowW - (2 * CELL_EDIT_RIGHT_MARGIN + 22 + 10))];
}

-(void)buttonClick{
    
    if (self.clickHandler) {
        self.clickHandler();
    }
    
}

-(UIButton *)button{
    
    if (_button == nil) {
        _button = [[UIButton alloc] init];
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
    
}



@end
